//
//  CustomViewController.m
//  LiTingFeng
//
//  Created by xinxuan on 16/1/20.
//  Copyright © 2016年 Bullgo. All rights reserved.
//

#import "CustomViewController.h"
#import "ASValueTrackingSlider.h"
#import "AlertViewController.h"
#import "MissView.h"
#import "CostomerModel.h"
#import "CostomManager.h"
#define TOTAL 10
#define LIMIT 2
#define YDIMG(__name) [UIImage imageNamed:__name]
static NSInteger level1 = 2;
static NSInteger level2 = 20;
static NSInteger level3 = 4;
@interface CustomViewController ()
@property (nonatomic,assign) NSInteger  liLiaoValue;
@property (nonatomic,assign) NSInteger  reFuValue;
@property (nonatomic,assign) NSInteger  anMoValue;
@property (weak, nonatomic) IBOutlet UISlider *liliaoslider;
@property (weak, nonatomic) IBOutlet UISlider *refuslider;
@property (weak, nonatomic) IBOutlet UISlider *anmoslider;
- (IBAction)liliaoSwtch:(id)sender;
- (IBAction)refuSwtch:(UISwitch *)sender;
- (IBAction)anmoSwtch:(id)sender;
@property (weak, nonatomic) IBOutlet UISwitch *liliaoSwtch;
@property (weak, nonatomic) IBOutlet UISwitch *refuSwtch;
@property (weak, nonatomic) IBOutlet UISwitch *anmoSwtch;

@property (nonatomic, strong)MissView *missView;

@property (nonatomic, assign)BOOL canThrough; /**<允许超过最大幅度*/
@property (nonatomic, assign)BOOL isShow; /**<判断当前Alert是否显示*/
@property (nonatomic, assign)NSInteger oldValue;


@end

@implementation CustomViewController


-(void)viewDidLoad{
    //标题
    self.navigationItem.title = self.name;
    [self layOut];
    _liliaoslider.tag = 1000;
    _liliaoslider.minimumValue = 0.0;
    _liliaoslider.value = 0.0;
    _liliaoslider.maximumValue = TOTAL;
    [self.view addSubview:_liliaoslider];
    self.canThrough = NO;
    self.isShow = NO;
    self.oldValue = 0;
    [self addBackgroudWithSlider:_liliaoslider];
    [self createViewWithSlider:_liliaoslider swtch:_liliaoSwtch];
    
    _refuslider.tag = 1001;
    _refuslider.minimumValue = 10.0;
    _refuslider.maximumValue = 60.0;
    [self addBackgroudWithSlider1:_refuslider];
    
    _anmoslider.tag = 1002;
    _anmoslider.minimumValue = 0.0;
    _anmoslider.value = 0.0;
    _anmoslider.maximumValue = 10.0;
    [self addBackgroudWithSlider2:_anmoslider];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"未选"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToSave)];
    if ([[NSUserDefaults standardUserDefaults]  boolForKey:@"已收藏"]){
        NSLog(@"%lu",(unsigned long)self.model.liLiaoSliderValue);
        _liliaoslider.value = self.model.liLiaoSliderValue;
        _refuslider.value = self.model.reFuSliderValue;
        _anmoslider.value = self.model.anMoSliderValue;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"已收藏"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
    
}


- (void) backToSave
{
  
    CostomerModel * model = [CostomerModel new];
    model.name = self.name;
    model.liLiaoSliderValue = self.liLiaoValue;
    model.reFuSliderValue = self.reFuValue;
    model.anMoSliderValue = self.anMoValue;
    

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"收藏成功" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    } ];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[CostomManager shareManager]insertData:model];
    }];
    [alert addAction:cancelAction];
    [alert addAction:okAction];
    
    [self presentViewController:alert animated:YES completion:nil];
  
}




- (void)block{
    
    
    NSLog(@"%@",@"页面见传值");
    
}

#pragma 根据不同的slider和开关

-(void)addBackgroudWithSlider:(UISlider*)slider{
   
    [slider setMaximumTrackImage:[UIImage imageNamed:@"progress_right"] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"理疗播放条"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateHighlighted];

}

-(void)addBackgroudWithSlider1:(UISlider*)slider{
    
    [slider setMaximumTrackImage:[UIImage imageNamed:@"progress_right"] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"热敷播放条"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateHighlighted];
    
}

-(void)addBackgroudWithSlider2:(UISlider*)slider{
    
    [slider setMaximumTrackImage:[UIImage imageNamed:@"progress_right"] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"按摩播放条"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"progress_point"] forState:UIControlStateHighlighted];
    
}
- (void)createViewWithSlider:(UISlider*)slider swtch:(UISwitch*)swtch{
    self.missView = [[MissView alloc] initWithFrame:slider.frame];
    __weak typeof(self) weakself = self;
    self.missView.block = ^(void) {
        [weakself adjustStateWithSwtch:swtch];
    };
    [self.view addSubview:self.missView];
    
    if (swtch.isOn) {
        [self.missView removeFromSuperview];
        self.missView = nil;
    }
}


- (void)adjustStateWithSwtch:(UISwitch*)swtch {
    [self showAlertController:@"请打开模式开关" who:0 swtch:swtch];
}



- (BOOL)canShow:(NSInteger)newValue level:(NSInteger)level {
    if (newValue < level) {
        self.canThrough = NO;
    }
    if (!self.canThrough && newValue > self.oldValue && !self.isShow) {
        self.oldValue = newValue;
        return YES;
    }
    else {
        self.oldValue = newValue;
        return NO;
    }
}


#pragma slider方法
- (IBAction)liliaoslider:(UISlider *)sender {
    NSInteger value = sender.value;
    self.liLiaoValue = value;
    if ([self canShow:value level:level1] && value >= level1) {
        [self showAlertController:@"幅度太大回趟啊" who:1 swtch:_liliaoSwtch];
    }
}
- (IBAction)refuslider:(UISlider *)sender {
    NSInteger value = sender.value;
     self.reFuValue = value;
    if ([self canShow:value level:level2] && value >= level2) {
        [self showAlertController:@"太热了" who:1 swtch: _refuSwtch];
    }
}

- (IBAction)anmoslider:(UISlider *)sender {
    NSInteger value = sender.value;
     self.anMoValue = value;
    if ([self canShow:value level:level3] && value >= level3) {
        [self showAlertController:@"幅度太大" who:1 swtch:_anmoSwtch];
    }
}

#pragma swith 方法

- (IBAction)liliaoSwtch:(id)sender {
    UISwitch * liliaoslider = (UISwitch*)sender;
    
    if (liliaoslider.isOn) {
        [self.missView removeFromSuperview];
    }
    else {
        [self createViewWithSlider:_liliaoslider swtch:_liliaoSwtch];
    }

}

- (IBAction)refuSwtch:(UISwitch *)sender {
}

- (IBAction)anmoSwtch:(id)sender {

}

#pragma 开始按钮
- (IBAction)begenBtn:(UIButton *)sender {
    
    NSLog(@"开始");
}


#pragma ballgo提示框

//提示框
- (void)showAlertController:(NSString *)message who:(NSInteger)who swtch:(UISwitch*)swtch
{
    self.isShow = YES;
    DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:message leftButtonTitle:@"" rightButtonTitle:@"确定"];
    [alert show];
    alert.leftBlock = ^() {
        NSLog(@"left button clicked");
        self.isShow = NO;
        if (who == 1) {
            UISlider *slider = (UISlider *)[self.view viewWithTag:1000];
            slider.value = 2;
        }
    };
    alert.rightBlock = ^() {
        NSLog(@"right button clicked");
        if (who == 0) {
            swtch.on = YES;
            [self.missView removeFromSuperview];
            self.missView = nil;
        }
        else if (who == 1) {
            self.canThrough = YES;
        }
        self.isShow = NO;
    };
    alert.dismissBlock = ^() {
        NSLog(@"Do something interesting after dismiss block");
    };
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;

}


-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    self.model = nil;
}


- (void)layOut{

    if (Screen_Width == 320 && Screen_Height == 480) {//苹果4
        
        self.refuUPSpace.constant = self.anmoUPSpace.constant = 60;
        self.refuImageSpace.constant = self.anmoImageSpace.constant = 46;
        self.refuSpotUpSpace.constant = self.anmoSpotUpSpace.constant = 110;
    }else if(Screen_Width == 320 && Screen_Height == 568){//苹果5
        self.refuUPSpace.constant = self.anmoUPSpace.constant = 60;
        self.refuImageSpace.constant = self.anmoImageSpace.constant = 46;
        self.refuSpotUpSpace.constant = self.anmoSpotUpSpace.constant = 110;

        
        
        
    }else if(Screen_Width == 375 && Screen_Height == 667){//苹果6
        
        
        
        
    }else{//苹果6puls及其以上
        
        
        
    }
}



@end
